package configuration;

import java.io.File;
import java.io.IOException;

import main.Main;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import userInterface.MainPage;

import fileProcessing.ParsingException;

public class UserFields_XmlReader {
	
	/**
	 * Parser for XML database file.
	 * 
	 * @throws SAXException
	 * @throws ParsingException 
	 * @throws IOException
	 */
	public UserFields_XmlReader() throws ParsingException {
		String programDirectory = "C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\WVU PII Parser\\source";
		String fieldFile = programDirectory+"\\configuration_files\\custom_fields.xml";
		String searchWordFile = programDirectory+"\\configuration_files\\search_words.xml";
		try {
			XMLReader reader = XMLReaderFactory.createXMLReader();
			reader.setContentHandler(new ContentHandler());
			if(new File(fieldFile).exists()){
				reader.parse(fieldFile);
			}
			if(new File(searchWordFile).exists()){
				reader.parse(searchWordFile);
			}
				
		} catch (IOException e) {
			throw new ParsingException(e.getMessage());
		} catch (SAXException e1) {
			throw new ParsingException(e1.getMessage());
		}
	}

	/**
	 * reads table meta data from XML document and creates custom field definitions.
	 * 
	 * @author ashepard
	 * 
	 */
	private class ContentHandler extends DefaultHandler {

		String fieldName="",fieldType="",aliases="",searchWord="";

		/**
		 * Parser for xml metadata
		 */
		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			int lineLength = attributes.getLength();
			if (qName.equalsIgnoreCase("field")) {
				for (int i = 0; i < lineLength; i++) {
					if (attributes.getQName(i).equalsIgnoreCase("name"))
						fieldName = attributes.getValue(i);
					else if (attributes.getQName(i).equalsIgnoreCase("type")){
						for(int j=i;j<lineLength;j++){
							if(j==lineLength-1){
								fieldType += attributes.getValue(j);
							}
							else{
								fieldType += (attributes.getValue(j)+",");
							}
						}
					}
					else if (attributes.getQName(i).equalsIgnoreCase("alias")){
						for(int j=i;j < lineLength;j++){
							if(j==lineLength-1){
								aliases += attributes.getValue(j);
							}
							else{
								aliases += (attributes.getValue(j)+",");
							}
						}
					}
				}
			}
			else if(qName.equalsIgnoreCase("word")) {
				for (int i = 0; i < lineLength; i++) {
					if (attributes.getQName(i).equalsIgnoreCase("name"))
						searchWord = attributes.getValue(i);
				}
			}
		}

		/**
		 * Adds parsed tables to database.
		 */
		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (qName.equalsIgnoreCase("field")) {
				MainPage.fieldMatcher.addNewCustomField(fieldName, fieldType, aliases);
			}
			else if(qName.equalsIgnoreCase("word")){
				MainPage.fieldMatcher.addNewSearchWord(searchWord);
			}
		}
	}
}
